﻿' ------------------------------------------------------------------------------
' One of the core functions of the application is to make database
' transactions and other internal workings of the application visible to
' the end user. The CoreEventSource class exposes a singleton object to clients
' that raises events which can be monitored and displayed to the user.
'
' The client accesses the singleton object via the static GetEventSource()
' method. It can attach event handlers to it that react to the events by
' displaying relevant information to the user.
'
' Internally, the assembly's methods call static methods on this
' class. Each static method call a corresponding method on the singleton
' which raises the event.
' ------------------------------------------------------------------------------
' This file is part of the SQL Server 2008 Technology Demonstration. This
' application was designed and coded by Eric Weitzman, LandTime Inc, for
' Microsoft Corporation. Contact Ed Katibah at Microsoft about this demo.
' Copyright © 2008 Microsoft Corporation. All rights reserved.
' ------------------------------------------------------------------------------

Public Class CoreEventSource

    Public Event Trace(ByVal msg As String)
    Public Event Debug(ByVal msg As String)
    Public Event SqlEvent(ByVal e As SqlEventArgs)
    Public Event Exception(ByVal ex As Exception)
    Public Event [Error](ByVal msg As String)
    Public Event StartTimer()
    Public Event ShowTimer(ByVal msg As String)

    Private Shared m_source As New CoreEventSource

    Private Sub New() ' inaccessible constructor, force use of GetRelay()
    End Sub

    Public Shared Function GetEventSource() As CoreEventSource
        Return m_source
    End Function

    Public Sub RaiseSqlEvent(ByVal e As SqlEventArgs)
        RaiseEvent SqlEvent(e)
    End Sub
    Public Sub RaiseException(ByVal ex As Exception)
        RaiseEvent Exception(ex)
    End Sub
    Public Sub RaiseError(ByVal msg As String)
        RaiseEvent [Error](msg)
    End Sub
    Public Sub RaiseTrace(ByVal msg As String)
        RaiseEvent Trace(msg)
    End Sub
    Public Sub RaiseDebug(ByVal msg As String)
        RaiseEvent Debug(msg)
    End Sub
    Public Sub RaiseStartTimer()
        RaiseEvent StartTimer()
    End Sub
    Public Sub RaiseShowTimer(ByVal msg As String)
        RaiseEvent ShowTimer(msg)
    End Sub

    Public Shared Sub ReportSqlCommand(ByVal msg As String, ByVal cmd As System.Data.SqlClient.SqlCommand)
        m_source.RaiseSqlEvent(New SqlEventArgs(msg, cmd))
    End Sub
    Public Shared Sub ReportSqlOperation(ByVal name As String, ByVal msg As String)
        m_source.RaiseSqlEvent(New SqlEventArgs(name, msg))
    End Sub
    Public Shared Sub ReportException(ByVal ex As Exception)
        m_source.RaiseException(ex)
    End Sub
    Public Shared Sub ReportError(ByVal msg As String)
        m_source.RaiseError(msg)
    End Sub
    Public Shared Sub ReportOpenSqlFilestreamError(ByVal errcode As UInt32)
        m_source.RaiseSqlEvent(New SqlEventArgs(errcode))
    End Sub
    Public Shared Sub ReportTrace(ByVal msg As String)
        m_source.RaiseTrace(msg)
    End Sub
    Public Shared Sub ReportDebug(ByVal msg As String)
        m_source.RaiseDebug(msg)
    End Sub
    Public Shared Sub ReportStartTimer()
        m_source.RaiseStartTimer()
    End Sub
    Public Shared Sub ReportShowTimer(ByVal msg As String)
        m_source.RaiseShowTimer(msg)
    End Sub

End Class
